const { chooseImage } = require('../../promise_api.js');

const app = getApp();
console.log(chooseImage)
Page({

  // 页面的初始数据
  data: {
    firstAvatar: '',
    secondAvatar: '',
    tempFilePaths: '',
  },

  // 生命周期函数--监听页面加载
  onLoad: function (options) {
    
  },

  bgStyle(key){
    let url = key;
    console.log('url',url)
    return  `background: url(${url}) no-repeat;
            background-size: contain;
            background-position: center;`;
  },

  // 开始匹配
  startMatching(){
    if (this.data.firstAvatar == '' || this.data.secondAvatar == ''){
      wx.showToast({
        title: '请上传图片',
        icon: 'none'
      })
      return
    }

    wx.showLoading({
      title: '识别中...',
    })

    const { firstAvatarUrl, secondAvatarUrl } = app.globalData;

    wx.cloud.callFunction({
      name: 'face',
      data: {
        firstAvatarUrl: firstAvatarUrl,
        secondAvatarUrl: secondAvatarUrl
      },
      success(res) {
        console.log('callFunction', res)

        app.globalData.score = res.result.Score;
        wx.hideLoading();

        wx.navigateTo({
          url: '../uploadresult/uploadresult',
        })

      },
      fail(err) {
        console.log(err)
        wx.showToast({
          title: '识别失败，请重新上传',
          icon: 'none'
        })
      }
    })
  },

  // 上传第一张图片
  getFirstAvatar(){
    this.doUpload().then((res) => {
      console.log('doload',res)
      if (res.errMsg != 'chooseImage:ok'){
        return
      }

      const filePath = res.tempFilePaths[0];
      const cloudPath = new Date().getTime() + filePath.match(/\.[^.]+?$/)[0];

      this.uploadFile(cloudPath, filePath).then((res) => {
        console.log('uploadFile',res)

        // 获取图片真实链接
        wx.cloud.getTempFileURL({
          fileList: [res.fileID],
          success: res => {
            console.log(res)
            app.globalData.firstAvatarUrl = res.fileList[0].tempFileURL;
            this.setData({
              firstAvatar: this.bgStyle(res.fileList[0].tempFileURL)
            })
          },
          fail: err => {
            console.log(err)
          },
          complete(){
            wx.hideLoading()
          }
        })
      })
    })
  },

  // 上传第二张图片
  getSecondAvatar(){
    this.doUpload().then((res) => {
      console.log('doUpload', res);
      if (res.errMsg == 'chooseImage:ok') {

        const filePath = res.tempFilePaths[0];
        const cloudPath = new Date().getTime() + filePath.match(/\.[^.]+?$/)[0];

        this.uploadFile(cloudPath, filePath).then((res) => {
          // 获取图片真实链接
          wx.cloud.getTempFileURL({
            fileList: [res.fileID],
            success: res => {
              app.globalData.secondAvatarUrl = res.fileList[0].tempFileURL;

              this.setData({
                secondAvatar: this.bgStyle(res.fileList[0].tempFileURL)
              })

            },
            fail: err => {
              console.log(err);
            },
            complete() {
              wx.hideLoading()
            }
          })
        })
      }
    })
  },

  // 选择图片
  doUpload() {
    return new Promise((resolve,reject) => {
      wx.chooseImage({
        count: 1,
        sizeType: ['compressed'],
        success(res) {

          resolve(res);

          wx.showLoading({
            title: '上传中',
            mask: true
          })
        },
      })
    })
  },

  // 上传图片到云储存空间
  uploadFile(cloudPath, filePath){
    return new Promise((resolve,reject) => {

      wx.cloud.uploadFile({
        cloudPath,
        filePath,
        success(res) {
          resolve(res);
        },
        fail(e) {
          wx.showToast({
            icon: 'none',
            title: '上传失败',
          })
        }
      })
    })
  }
})